/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { mockProjectFromResourcesPath } from '../common/init-project';
import { Module, Project } from '../../../../src/base/external/core/hvigor-core-node.js';
import { Task } from '@ohos/hvigor-base';
import { DefaultInternalTask } from '../../../../src/base/internal/task/core/default-internal-task';
import { noop } from '../../../../src/common/util/noop';
import { TaskQueue } from '../../../../src/base/internal/task/core/task-queue';

const project: Project = mockProjectFromResourcesPath('common-test');

describe('test task queue', () => {
  const module: Module = project.findModuleByName('module1')!;
  const task1: Task = new DefaultInternalTask(module, {
    name: 'task1'
  }, noop);
  const task2: Task = new DefaultInternalTask(module, {
    name: 'task2'
  }, noop);
  const task3: Task = new DefaultInternalTask(module, {
    name: 'task3'
  }, noop);

  test('test task queue push&pop&peek', () => {
    const taskQueue = new TaskQueue();
    taskQueue.push(task1);
    taskQueue.push(task2);
    expect(taskQueue.size()).toBe(2);

    taskQueue.push(task3);
    taskQueue.pop();
    const taskPop: Task | undefined = taskQueue.pop();
    expect(taskPop!.getName()).toBe('task2');
    expect(taskQueue.size()).toBe(1);

    const taskPeek = taskQueue.peek();
    expect(taskPeek!.getName()).toBe('task3');
    expect(taskQueue.size()).toBe(1);
  });

});
